//=============================================================================
// System  : MatchKit
// File    : InternalExclusivePattern.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using MatchKit.Patterns;

namespace MatchKit.ObjectMapping.Patterns
{
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	sealed class InternalExclusivePattern : BasePattern
	{
		#if DC
		[DataMember]
		#endif
		private IPattern[] _items;

		public InternalExclusivePattern(params IPattern[] items)
		{
			if (items == null)
				throw new ArgumentNullException("items", "items cannot be null");
			if (items.Length == 0)
				throw new ArgumentException("items must contains at least one element", "items");
			if (Utils.AnyNull(items))
				throw new ArgumentException("items cannot contains null elements", "items");
			_items = items;
		}

		public override IEnumerable<IPattern> Children
		{
			get { return _items; }
		}

		public override bool HasChildren
		{
			get { return true; }
		}

		protected override Match OnMatch(MatchContext context)
		{
			#region tracer
			var tracer = context.Tracer;
			int tryNo = 0;
			#endregion

			foreach (var item in _items)
			{
				if (item == null)
					throw new MatchKitException(29, "Exclusive pattern cannot contains null children elements");

				#region tracer
				if (tracer != null)
				{
					IMatchTracerExtensions.BeginMatch(tracer, context, item, "Begin Try #{0} : {1}", ++tryNo, item.HelpString);
				}
				#endregion

				var branch = context.Branch();
				var m = item.Match(branch);

				if (m.Success)
				{
					#region tracer
					if (tracer != null)
					{
						IMatchTracerExtensions.MatchComplete(tracer, context, m, "Success with Try #{0}", tryNo);
					}
					#endregion

					context.Commit(branch);
					return m;
				}

				#region tracer
				if (tracer != null)
				{
					IMatchTracerExtensions.MatchComplete(tracer, context, m, "Error with Try #{0}", tryNo);
				}
				#endregion
			}

			// se arrivo qui nessuna possibilita' e' andata in matched
			return Error(context);
		}

		public override string HelpString
		{
			get
			{
				var sb = new StringBuilder(255);
				foreach (var item in _items)
				{
					if (sb.Length != 0)
						sb.Append(" || ");
					sb.Append(item.HelpString);
				}
				return "One of (" + sb + ")";
			}
		}
	}
}
